<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta charset="utf-8">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head>
<body>

<p id="horizontalParagraph">
one two אחתשתיים three four שלושהארבעה
</p>

<p id="verticalParagraph" style="-webkit-writing-mode: vertical-lr">
one two אחתשתיים three four שלושהארבעה
</p>

<script>
function parseObject(str) {
    return Function('"use strict"; return (' + str  + ');')();
}

// For several possible words in the text, get the bounds of the word in the accessibility
// tree, and also in the DOM, and assert that they're exactly the same.
function testWord(elementId, word) {
    var paragraph = document.getElementById(elementId);
    var domText = paragraph.innerHTML;

    // Get the bounds from the accessibility tree.
    var axParagraph = accessibilityController.accessibleElementById(elementId);
    var axStaticText = axParagraph.childAtIndex(0);
    var text = axStaticText.name;
    assert_not_equals(axStaticText, null);
    var wordAxIndex = text.indexOf(word);
    assert_false(wordAxIndex < 0);
    var axBounds = parseObject(axStaticText.boundsForRange(wordAxIndex, wordAxIndex + word.length));

    // Get the bounds from the DOM.
    var domIndex = domText.indexOf(word);
    var range = new Range();
    range.setStart(paragraph.firstChild, domIndex);
    range.setEnd(paragraph.firstChild, domIndex + word.length);
    var rangeBounds = range.getBoundingClientRect();

    // Make sure they're the same.
    assert_approx_equals(axBounds.x, rangeBounds.left, 2);
    assert_approx_equals(axBounds.y, rangeBounds.top, 2);
    assert_approx_equals(axBounds.width, rangeBounds.width, 2);
    assert_approx_equals(axBounds.height, rangeBounds.height, 2);
}

test((t) => {
    testWord('horizontalParagraph', 'one');
    testWord('horizontalParagraph', 'two');
    testWord('horizontalParagraph', 'three');
    testWord('horizontalParagraph', 'four');
    testWord('horizontalParagraph', 'אחתשתיים');
    testWord('horizontalParagraph', 'שלושהארבעה');

    testWord('verticalParagraph', 'one');
    testWord('verticalParagraph', 'two');
    testWord('verticalParagraph', 'three');
    testWord('verticalParagraph', 'four');
    testWord('verticalParagraph', 'אחתשתיים');
    testWord('verticalParagraph', 'שלושהארבעה');
}, "Tests that we can compute the bounds of a range of text from the accessibility tree in bidirectional text.");

</script>

</body>
</html>
